#ifndef COMMON_H
#define COMMON_H

#include <openssl/sm2.h>
#include <openssl/sm3.h>
#include <openssl/sm4.h>
#include <stdint.h>
#include <stddef.h>

#define MAX_USERNAME_LEN 32
#define MAX_PASSWORD_LEN 64
#define MAX_MSG_LEN 1024
#define SERVER_PORT 8888


typedef enum {
	    MSG_TYPE_LOGIN = 1,
	        MSG_TYPE_LOGIN_RESP,
		    MSG_TYPE_CHAT,
		        MSG_TYPE_LOGOUT,
			    MSG_TYPE_ERROR
} MessageType;

typedef struct {
	    uint32_t msg_type;
	        uint32_t msg_len;
} MessageHeader;

typedef struct {
	    char username[MAX_USERNAME_LEN];
	        char password_hash[SM3_DIGEST_LENGTH];
} LoginRequest;

typedef struct {
	    uint8_t success;
	        char salt[SM3_DIGEST_LENGTH];
} LoginResponse;

typedef struct {
	    char target_user[MAX_USERNAME_LEN];
	        uint8_t encrypted_msg[MAX_MSG_LEN];
		    uint32_t msg_len;
} ChatMessage;

void compute_sm3_hash(const char *password, const char *salt, unsigned char *hash);
void generate_salt(char *salt, size_t len);
void sm4_encrypt(const unsigned char *key, const unsigned char *plaintext, 
		                unsigned char *ciphertext, size_t len);
void sm4_decrypt(const unsigned char *key, const unsigned char *ciphertext, 
		                unsigned char *plaintext, size_t len);

#endif // COMMON_H
